package tictactoe.Domain;

/**
 * Created with IntelliJ IDEA.
 * User: Krilli
 * Date: 24.11.2012
 * Time: 14:04
 * To change this template use File | Settings | File Templates.
 */
public class Game{

    private int count = 0;
    private String[] state = new String[9];
    private final int[][] solutions = {
            {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, // horizontal solutions
            {0, 3, 6}, {1, 4, 7}, {2, 5, 8},  // vertical solutions
            {0, 4, 8}, {2, 4, 6}             // diagonal solutions
    };

    public Game()
    {
        for(int i = 0; i < state.length; i++)
            state[i] = "";
    }

    public String getPlayer() {
        if(count % 2 == 0){
            return "O";
        }else{
            return "X";
        }
    }

    public void changeState(int index) throws ArrayIndexOutOfBoundsException
    {
        ++count;
        String letter = getPlayer();

        state[index]= letter;
    }

    public String getState(int index){
        return state[index];
    }

    public int getCount() {
        return count;
    }

    public boolean fullBoard() {
        for (String aState : state) {
            if (aState.equals(""))
                return false;
        }
        return true;
    }

    public boolean checkForWin()
    {
        for (int[] solution : solutions) {
            if (state[solution[0]].equals(state[solution[1]])
                    && state[solution[1]].equals(state[solution[2]])
                    && !(state[solution[0]].equals(""))) {
                return true;
            }
        }
        return false;
    }
}
